<template>
  <div>
    <div v-if="playType === 'video'">
      <div v-if="!isPlay" v-loading="true" :style="{'width': width,'height':height}" style="background: black" />
      <video-player
        v-else
        :height="height"
        :width="width"
        :options="videoOptions"
      />
    </div>
    <el-image
      v-else
      :src="picturePath"
      :preview-src-list="[picturePath]"
      fit="fit"
      :style="{'width': width,'height':height}"
    />
    <div style="display: flex;justify-content: space-between;margin-top: 10px">
      <div>
        <el-button
          v-for="(item,index) in playTypeList"
          :key="index"
          style="margin-right: 5px"
          :plain="playType !== item.value"
          type="primary"
          @click="playTypeChange(item.value)"
        >{{ item.label }}
        </el-button>
      </div>
      <div>
        <el-button
          v-if="playType === 'picture'"
          icon="el-icon-refresh"
          type="primary"
          @click="refreshPicture"
        />
        <el-button
          type="danger"
          icon="el-icon-video-pause"
          @click="closePlay"
        />
        <el-button
          type="primary"
          :icon="showTool ? 'el-icon-caret-top' : 'el-icon-caret-bottom'"
          @click="showTool = !showTool"
        />
      </div>
    </div>

    <div v-if="showTool" style="margin-top:13px;display: flex;justify-content: space-between">
      <div style="width: 120px;">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-button
              plain
              type="primary"
              icon="el-icon-top-left"
              circle
              @click="control('ptzUpLeft')"
            />
          </el-col>
          <el-col :span="8">
            <el-button
              plain
              type="primary"
              icon="el-icon-top"
              circle
              @click="control('ptzUp')"
            />
          </el-col>
          <el-col :span="8">
            <el-button
              plain
              type="primary"
              icon="el-icon-top-right"
              circle
              @click="control('ptzUpRight')"
            />
          </el-col>
        </el-row>
        <el-row :gutter="20" style="margin-top: 13px">
          <el-col :span="8">
            <el-button
              plain
              type="primary"
              icon="el-icon-back"
              circle
              @click="control('ptzLeft')"
            />
          </el-col>
          <el-col :span="8">
            <el-tooltip class="item" effect="dark" :content="$t('stop')" placement="top-start">
              <el-button
                plain
                type="primary"
                icon="el-icon-lock"
                circle
                @click="control('ptzStop')"
              />
            </el-tooltip>
          </el-col>
          <el-col :span="8">
            <el-button
              plain
              type="primary"
              icon="el-icon-right"
              circle
              @click="control('ptzRight')"
            />
          </el-col>
        </el-row>
        <el-row :gutter="20" style="margin-top: 13px">
          <el-col :span="8">
            <el-button
              plain
              type="primary"
              icon="el-icon-bottom-left"
              circle
              @click="control('ptzDownLeft')"
            />
          </el-col>
          <el-col :span="8">
            <el-button
              plain
              type="primary"
              icon="el-icon-bottom"
              circle
              @click="control('ptzDown')"
            />
          </el-col>
          <el-col :span="8">
            <el-button
              plain
              type="primary"
              icon="el-icon-bottom-right"
              circle
              @click="control('ptzDownRight')"
            />
          </el-col>
        </el-row>
      </div>
      <div>
        <div style="width: 170px;display: flex;justify-content: space-around">
          <el-tooltip
            class="item"
            effect="dark"
            :content="$t('aroundHorizontal')"
            placement="top-start"
          >
            <el-button
              plain
              type="danger"
              icon="el-icon-refresh"
              circle
              @click="control('aroundHorizontal')"
            />
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            :content="$t('around')"
            placement="top-start"
          >
            <el-button
              plain
              type="danger"
              icon="el-icon-rank"
              circle
              @click="control('around')"
            />
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            :content="$t('aroundVertical')"
            placement="top-start"
          >
            <el-button
              plain
              type="danger"
              icon="el-icon-sort"
              circle
              @click="control('aroundVertical')"
            />
          </el-tooltip>
        </div>
      </div>
      <div>
        <div>
          <el-tooltip
            class="item"
            effect="dark"
            :content="$t('focusIn')"
            placement="top-start"
          >
            <el-button
              plain
              type="info"
              icon="el-icon-zoom-in"
              circle
              @click="control('zoomIn')"
            />
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            :content="$t('focusOut')"
            placement="top-start"
          >
            <el-button
              plain
              type="info"
              icon="el-icon-zoom-out"
              circle
              @click="control('zoomOut')"
            />
          </el-tooltip>
        </div>
        <div style="margin-top: 13px">
          <el-tooltip
            class="item"
            effect="dark"
            :content="$t('zoomIn')"
            placement="top-start"
          >
            <el-button
              plain
              type="info"
              icon="el-icon-plus"
              circle
              @click="control('ptzZoomIn')"
            />
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            :content="$t('zoomOut')"
            placement="top-start"
          >
            <el-button
              plain
              type="info"
              icon="el-icon-minus"
              circle
              @click="control('ptzZoomOut')"
            />
          </el-tooltip>
        </div>
        <div style="margin-top: 13px">
          <el-tooltip
            class="item"
            effect="dark"
            :content="$t('flip')"
            placement="top-start"
          >
            <el-button
              plain
              type="info"
              icon="el-icon-refresh"
              circle
              @click="control('ptzFlip')"
            />
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            :content="$t('mirror')"
            placement="top-start"
          >
            <el-button
              plain
              type="info"
              icon="el-icon-guide"
              circle
              @click="control('ptzMirror')"
            />
          </el-tooltip>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  successNotice
} from '@/util/common'
import VideoPlayer from '@/components/VideoPlayer'
import {
  pushCloud,
  stopCloud,
  controlAction,
  keepPushCloud
} from '@/api/camera/cameraDeviceApi'

export default {
  name: 'CameraPlayer',
  components: {
    VideoPlayer
  },
  props: {
    deviceId: {
      type: Number,
      default: null
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '280px'
    },
    type: {
      type: String,
      default: 'video'
    }
  },
  data() {
    return {
      playUrl: null,
      isPlay: false,
      timer: '',
      showTool: false,
      playTypeList: [],
      videoOptions: null,
      playType: null,
      picturePath: null
    }
  },
  watch: {
    type: {
      deep: true,
      handler() {
        this.playTypeChange(this.type)
      }
    }
  },
  mounted() {
    this.playTypeList = [
      {
        value: 'video',
        label: this.$t('video')
      }, {
        value: 'picture',
        label: this.$t('snap')
      }
    ]
    this.playTypeChange(this.type)
  },
  beforeDestroy() {
    this.closePlay()
  },
  methods: {
    closePlay() {
      this.stopPlay()
      this.$emit('onStop', {})
    },
    playTypeChange(type) {
      if (type === 'picture' && this.playType === 'video') {
        this.stopPlay()
        this.refreshPicture()
      } else if (type === 'video' && this.playType === 'picture') {
        this.pushCloud()
      } else if (!this.playType) {
        if (type === 'picture') {
          this.stopPlay()
          this.refreshPicture()
        } else if (type === 'video') {
          this.pushCloud()
        }
      }
      this.playType = type
    },
    refreshPicture() {
      this.picturePath = window.VUE_APP_BASE_API + '/camera/device/snap?deviceId=' + this.deviceId + '&date=' + new Date()
    },
    keepPushCloud() {
      if (this.isPlay) {
        keepPushCloud({
          deviceId: this.deviceId,
          mediaType: 'hls'
        })
      }
    },
    pushCloud() {
      const data = {
        deviceId: this.deviceId,
        mediaType: 'hls'
      }
      pushCloud(data).then(res => {
        const { data } = res
        if (data.playUrl) {
          this.videoOptions = {
            autoplay: true,
            controls: true,
            sources: [{
              src: data.playUrl,
              type: 'application/x-mpegURL'
            }]
          }
          this.isPlay = true
          this.timer = setInterval(this.keepPushCloud, 20000)
        }
      })
    },
    control(action) {
      controlAction({
        deviceId: this.deviceId,
        action: action
      }).then(res => {
        const { code } = res
        if (code === '200') {
          successNotice(this)
        }
      })
    },
    stopPlay() {
      this.isPlay = false
      clearInterval(this.timer)
      const data = {
        deviceId: this.deviceId
      }
      stopCloud(data)
    }

  }
}
</script>

